Skip to content

Method: lambda$childrenPm$2(String, Optional, SimpleComposite)

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.javafx.impl.common;
27:
28: import javax.annotation.Nonnegative;
29: import jakarta.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.List;
32: import java.util.concurrent.Executor;
33: import java.util.concurrent.RejectedExecutionException;
34: import java.util.function.Consumer;
35: import java.util.function.Supplier;
36: import javafx.collections.ObservableList;
37: import javafx.application.Platform;
38: import it.tidalwave.ui.core.role.PresentationModel;
39: import it.tidalwave.ui.javafx.impl.Logging;
40: import it.tidalwave.util.As;
41: import it.tidalwave.role.Composite;
42: import it.tidalwave.role.SimpleComposite;
43: import lombok.experimental.UtilityClass;
44: import lombok.extern.slf4j.Slf4j;
45: import static it.tidalwave.ui.javafx.impl.Logging.INDENT;
46: import static java.util.Collections.emptyList;
47: import static javafx.collections.FXCollections.*;
48:
49: /***************************************************************************************************************************************************************
50: *
51: * @author Fabrizio Giudici
52: *
53: **************************************************************************************************************************************************************/
54: @UtilityClass @Slf4j
55: public class JavaFXWorker
56: {
57: private static final As.Type<SimpleComposite<PresentationModel>> _PresentationModel_Composite_ = As.type(Composite.class);
58:
59: public static <T> void run (@Nonnull final Executor executor, @Nonnull final Supplier<T> backgroundSupplier, @Nonnull final Consumer<T> javaFxFinalizer)
60: {
61: try
62: {
63: executor.execute(() ->
64: {
65: final var value = backgroundSupplier.get();
66: Platform.runLater(() -> javaFxFinalizer.accept(value));
67: log.trace("JavaFX finalizer submitted");
68: });
69: }
70: catch (RejectedExecutionException e)
71: {
72: log.error("Background task failed: {}", e.getMessage());
73: }
74: }
75:
76: @Nonnull
77: public static ObservableList<PresentationModel> childrenPm (@Nonnull final PresentationModel pm)
78: {
79: return childrenPm(pm, 0);
80: }
81:
82: @Nonnull
83: public static ObservableList<PresentationModel> childrenPm (@Nonnull final PresentationModel pm, @Nonnegative final int depth)
84: {
85: final var indent = INDENT.substring(0, depth * 8);
86: final var composite = pm.maybeAs(_PresentationModel_Composite_);
87: composite.ifPresent(c -> Logging.logObject(indent, composite));
88: final var items = composite.map(c -> c.findChildren().results()).orElse(emptyList());
89: final var badItems = extractBadItems(items);
90:
91: if (!badItems.isEmpty()) // defensive
92: {
93: log.error("Child object are not PresentationModel: (only 10 are shown)");
94: log.error("This happens when the PresentationModel doesn't have its own Composite role that decorates the" +
95: " owner entity Composite - see SimpleCompositePresentable for instance.");
96: badItems.stream().limit(10).forEach(item -> log.error(" {}", item));
97: return emptyObservableList();
98: }
99:
100: Logging.logObjects(indent, items);
101: return observableArrayList(items);
102: }
103:
104: @Nonnull
105: private static List<Object> extractBadItems (@Nonnull final List<PresentationModel> items)
106: {
107: final List<Object> badItems = new ArrayList<>(items);
108: badItems.removeIf(PresentationModel.class::isInstance);
109: return badItems;
110: // return items.stream()
111: // .map(item -> (Object)item)
112: // .filter(item -> !(item instanceof PresentationModel))
113: // .peek(item -> log.error(">>>> {}", item))
114: // .collect(toList());
115: }
116: }
117: